package com.accesshollywood.grfxOrdering.panels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.components.FoldablePanel;
import com.accesshollywood.grfxOrdering.basics.Colors;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.GrfxOrder;

public class ApprovalPanel extends JPanel {
	public JComponent				parent;
	public GrfxOrder				grfxOrder;
	int								needsBitMask	= 0;
	int								apprBitMask		= 0;

	public static final boolean		VERBOSE			= true;
	public static final boolean		NOT_VERBOSE		= false;

	public static final String[]	verbose			= { "Research", "PhotoEdit", "Footage", "Legal" };
	public static final String[]	not_verbose		= { "R", "Ph", "F", "L" };

	public static final int			flagResearch	= 1;												// 2^0 000
	public static final int			flagPhotoEdit	= 2;												// 2^1 001
	public static final int			flagFootage		= 4;												// 2^2 010
	public static final int			flagLegal		= 8;												// 2^3 100

	JPanel							checkPanel;
	public JCheckBox				checkResearch;
	public JCheckBox				checkPhotoEdit;
	public JCheckBox				checkFootage;
	public JCheckBox				checkLegal;
	JPanel							grabClosePanel;
	public JButton					grabOrder;
	public JButton					completeOrder;
	JCheckBox						closeOrder;
	JCheckBox						archiveOrder;

	public ApprovalPanel(GrfxOrder grfxOrder, JComponent parent) {
		this.grfxOrder = grfxOrder;
		this.parent = parent;

		apprBitMask = grfxOrder.calcApprBitMask();

		needsBitMask = grfxOrder.calcNeedsBitMask();

		createPanel(true);
	}

	public void createPanel(boolean isVerbose) {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		// setLayout(new HorizontalLayout());
		setBorder(new TitledBorder(new EtchedBorder(), ""));

		checkPanel = new JPanel();
		checkPanel.setAlignmentX(RIGHT_ALIGNMENT);
		checkPanel.setMaximumSize(new Dimension(32767, 25));
		checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.X_AXIS));
		add(checkPanel);

		String[] labels;

		if (isVerbose) {
			labels = verbose;
		} else {
			labels = not_verbose;
		}

		if ((needsBitMask & flagResearch) != 0) {
			checkResearch = setupCheckBox(labels[0], (apprBitMask & flagResearch) != 0);
			checkResearch.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isResearch());
			checkPanel.add(checkResearch);
		}
		if ((needsBitMask & flagPhotoEdit) != 0) {
			checkPhotoEdit = setupCheckBox(labels[1], (apprBitMask & flagPhotoEdit) != 0);
			checkPhotoEdit.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isPhotoEdit());
			checkPanel.add(checkPhotoEdit);
		}
		if ((needsBitMask & flagFootage) != 0) {
			checkFootage = setupCheckBox(labels[2], (apprBitMask & flagFootage) != 0);
			checkFootage.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isFootage());
			checkPanel.add(checkFootage);
		}
		if ((needsBitMask & flagLegal) != 0) {
			checkLegal = setupCheckBox(labels[3], (apprBitMask & flagLegal) != 0);
			checkLegal.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isLegal());
			checkPanel.add(checkLegal);
		}
		grabClosePanel = new JPanel();
		grabClosePanel.setAlignmentX(RIGHT_ALIGNMENT);
		grabClosePanel.setLayout(new BoxLayout(grabClosePanel, BoxLayout.X_AXIS));

		closeOrder = new JCheckBox("Closed");
		archiveOrder = new JCheckBox("Archived");
		closeOrder.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isOrderer());
		archiveOrder.setEnabled(Users.currentUser.isAdmin() || Users.currentUser.isOrderer());

		grabClosePanel.add(closeOrder);
		grabClosePanel.add(archiveOrder);
		add(grabClosePanel);

		if (Users.currentUser.isAdmin() || Users.currentUser.isArtist()) {
			completeOrder = new JButton("Mark Complete");
			completeOrder.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					grfxOrder.setComplete(true);
					OrderDetailsPanel p1 = (OrderDetailsPanel) parent;
					FoldablePanel p2 = (FoldablePanel) p1.parent;
					OrderHeaderPanel header = (OrderHeaderPanel) p2.headerPanel;
					((JButton) header.getComponent(0)).doClick();
				}
			});
		}
		if ((grfxOrder.getArtist() != null && grfxOrder.getArtist().getId() > 20) || grfxOrder.isComplete() || grfxOrder.isClosed() || grfxOrder.isArchived()) {
			if (grfxOrder.getArtist() != Users.currentUser) {
			}
			if (completeOrder != null) {
				completeOrder.setEnabled(false);
			}
		} else {
			if (completeOrder != null) {
				completeOrder.setEnabled(true);
			}
		}

		if (grabOrder != null) {
			grabClosePanel.add(grabOrder);
		}

		if (completeOrder != null) {
			grabClosePanel.add(completeOrder);
		}

		//closeOrder.setEnabled(false);
		//archiveOrder.setEnabled(false);

		closeOrder.setSelected(grfxOrder.isClosed());
		closeOrder.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				boolean isClosed = (e.getStateChange() == ItemEvent.SELECTED);
				grfxOrder.setClosed(isClosed);
				setBackgroundColors();
			}
		});

		setBackgroundColors();
		archiveOrder.setSelected(grfxOrder.isArchived());
		archiveOrder.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				boolean isArchived = (e.getStateChange() == ItemEvent.SELECTED);
				grfxOrder.setArchived(isArchived);
				setBackgroundColors();
			}
		});

		if (Users.currentUser.isAdmin()) {
			closeOrder.setEnabled(true);
			archiveOrder.setEnabled(true);
		}

		addResearchListener(new ApprItemListener(ApprovalPanel.flagResearch));
		addPhotoEditListener(new ApprItemListener(ApprovalPanel.flagPhotoEdit));
		addFootageListener(new ApprItemListener(ApprovalPanel.flagFootage));
		addLegalListener(new ApprItemListener(ApprovalPanel.flagLegal));
		setBackgroundColors();

		updateCompleteButton();
	}

	public void refresh() {
		setBackgroundColors();
		// refreshChecks();
		revalidate();
		repaint();
	}

	public void updateCompleteButton() {
		if (completeOrder != null) {
			boolean appr = isApprReady(grfxOrder.calcApprBitMask(), grfxOrder.calcNeedsBitMask());
			if (appr && (Users.currentUser.isAdmin() || Users.currentUser == grfxOrder.getArtist()) && !(grfxOrder.isComplete() || grfxOrder.isClosed() || grfxOrder.isArchived())) {
				completeOrder.setEnabled(true);
			} else {
				completeOrder.setEnabled(false);
			}
		}
	}

	public void setBackgroundsIfReady() {
		boolean appr = isApprReady(grfxOrder.calcApprBitMask(), grfxOrder.calcNeedsBitMask());

		this.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
		checkPanel.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
		grabClosePanel.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
		if (completeOrder != null) {
			// completeOrder.setEnabled(appr && !(model.artist != null || model.complete || model.closed || model.archived));
			updateCompleteButton();
		}

		this.getComponent(0).setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));

		OrderDetailsPanel p1 = (OrderDetailsPanel) parent;
		FoldablePanel p2 = (FoldablePanel) p1.parent;
		OrderHeaderPanel header = (OrderHeaderPanel) p2.headerPanel;
		if (header != null) {
			OrderTree2.setBackgroundIfReady(header, grfxOrder.getState());
		}
	}

	public void refreshChecks() {
		if (checkResearch != null) {
			checkResearch.setSelected(grfxOrder.isApprResearch());
		}
		if (checkPhotoEdit != null) {
			checkPhotoEdit.setSelected(grfxOrder.isApprPhotoEdit());
		}
		if (checkFootage != null) {
			checkFootage.setSelected(grfxOrder.isApprFootage());
		}
		if (checkLegal != null) {
			checkLegal.setSelected(grfxOrder.isApprLegal());
		}
	}

	public void setBackgroundColors() {
		boolean appr = isApprReady(grfxOrder.calcApprBitMask(), grfxOrder.calcNeedsBitMask());
		boolean closed = grfxOrder.isClosed();
		boolean complete = grfxOrder.isComplete();
		boolean archived = grfxOrder.isArchived();
		Color c;

		if (archived) {
			c = Colors.getColor(Colors.ARCHIVED);
		} else if (closed) {
			c = Colors.getColor(Colors.CLOSED);
		} else if (complete) {
			c = Colors.getColor(Colors.COMPLETED);
		} else if (appr) {
			c = Colors.getColor(Colors.APPROVED);
		} else {
			c = Colors.getColor(Colors.NOT_READY);
		}

		this.setBackground(c);
		checkPanel.setBackground(c);
		grabClosePanel.setBackground(c);

		if (completeOrder != null) {
			// completeOrder.setEnabled(appr && !(model.artist != null || model.complete || model.closed || model.archived));
			updateCompleteButton();
		}

		this.getComponent(0).setBackground(c);

		OrderDetailsPanel p1 = (OrderDetailsPanel) parent;
		FoldablePanel p2 = (FoldablePanel) p1.parent;
		OrderHeaderPanel header = (OrderHeaderPanel) p2.headerPanel;
		if (header != null) {
			// OrderTree2.setBackgroundColors(c, header, model.getState());
			header.setBackground(c);
		}
	}

	public boolean isApprReady(int apprBitMask, int needsBitMask) {
		if ((needsBitMask & flagResearch) != 0 && (apprBitMask & flagResearch) == 0) {
			return false;
		}
		if ((needsBitMask & flagPhotoEdit) != 0 && (apprBitMask & flagPhotoEdit) == 0) {
			return false;
		}
		if ((needsBitMask & flagFootage) != 0 && (apprBitMask & flagFootage) == 0) {
			return false;
		}
		if ((needsBitMask & flagLegal) != 0 && (apprBitMask & flagLegal) == 0) {
			return false;
		}
		return true;
	}

	private JCheckBox setupCheckBox(String label, boolean appr) {
		JCheckBox check = new JCheckBox(label);

		check.setSelected(appr);

		check.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));

		return check;
	}

	public void addResearchListener(ItemListener itemListener) {
		if (checkResearch != null) {
			checkResearch.addItemListener(itemListener);
		}
	}

	public void addPhotoEditListener(ItemListener itemListener) {
		if (checkPhotoEdit != null) {
			checkPhotoEdit.addItemListener(itemListener);
		}
	}

	public void addFootageListener(ItemListener itemListener) {
		if (checkFootage != null) {
			checkFootage.addItemListener(itemListener);
		}
	}

	public void addLegalListener(ItemListener itemListener) {
		if (checkLegal != null) {
			checkLegal.addItemListener(itemListener);
		}
	}

	public void setCheck(int apprBitMask, boolean state) {
		if ((apprBitMask & ApprovalPanel.flagResearch) != 0) {
			grfxOrder.setApprResearch(state);
		}
		if ((apprBitMask & ApprovalPanel.flagPhotoEdit) != 0) {
			grfxOrder.setApprPhotoEdit(state);
		}
		if ((apprBitMask & ApprovalPanel.flagFootage) != 0) {
			grfxOrder.setApprFootage(state);
		}
		if ((apprBitMask & ApprovalPanel.flagLegal) != 0) {
			grfxOrder.setApprLegal(state);
		}
	}

	public class ApprItemListener implements ItemListener {
		int	whichBitMask;

		public ApprItemListener(int bitMask) {
			this.whichBitMask = bitMask;
		}

		@Override
		public void itemStateChanged(ItemEvent itemEvent) {
			grfxOrder.setDeepAccess(true);
			// //Trace.info("itemStateChanged");
			String field = "";
			if ((whichBitMask & ApprovalPanel.flagResearch) != 0) {
				field = "apprResearch";
			}
			if ((whichBitMask & ApprovalPanel.flagPhotoEdit) != 0) {
				field = "apprPhotoEdit";
			}
			if ((whichBitMask & ApprovalPanel.flagFootage) != 0) {
				field = "apprFootage";
			}
			if ((whichBitMask & ApprovalPanel.flagLegal) != 0) {
				field = "apprLegal";
			}
			JCheckBox source = (JCheckBox) itemEvent.getItemSelectable();
			source.setBackground((itemEvent.getStateChange() == ItemEvent.SELECTED) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
			// setBackgroundsIfReady();
			setBackgroundColors();

			//GFXSQL.setAppr(model, field, ((JCheckBox) itemEvent.getItem()).isSelected());

			// GRFXOrdersMain.serverSocket.sendToClients(GfxProtocol.COMMAND.REFRESH, model.getIdOrder() + "");

			setCheck(whichBitMask, ((JCheckBox) itemEvent.getItem()).isSelected());
			grfxOrder.setDeepAccess(false);
		}
	}
}
